package com.thti.mecool.client.widget;

import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.app.AlertDialog;

public class AlterDialogWidget {
	private AlertDialog.Builder dialog;
	private Context context;
	private TextView textView,clearStore;
	private Button button;
	private String[] items;
	private int which=-1;
	private EditText et;
	private IsOKListener isOKListener;
	private OnClickListener listener = new OnClickListener() {
		@Override
		public void onClick(DialogInterface dialog, int which) {
			dialog.dismiss();
			setWhich(which);
			if (textView != null) {
				if(items!=null)
				textView.setText(item(which));

				judgeIsOK();
			} else if (button != null) {
				if(items!=null)
				button.setText(item(which));
			}
		}
	};
	private void judgeIsOK(){
		if(isOKListener!=null){
			if(et!=null)
			isOKListener.isOk(true,et.getText().toString());
			else if(textView!=null){
				isOKListener.isOk(true,textView.getText().toString());
			}
		}
	}
	public void setIsOKListener(IsOKListener isOKListener){
		this.isOKListener=isOKListener;
	}
	private OnClickListener EditListener = new OnClickListener() {
		
		@Override
		public void onClick(DialogInterface dialog, int which) {
			switch (which) {
			case -1:
				if(textView!=null){
					textView.setText(et.getText());
					if(!textView.isShown())
						textView.setVisibility(0);
					judgeIsOK();
				}
				break;
			case -2:
				dialog.dismiss();
				break;
			}
		}
	};
	
		
	public AlterDialogWidget(Context context) {
		this.context = context;
		init();
	}
	
	public void setWhich(int which) {
			this.which = which;
	
	}
	/**
	* @param context
	* @param title
	* @param items
	* @param ReturnView
	*/
	public AlterDialogWidget(Context context, String title, String[] items ,View ReturnView) {
		this.context = context;
		init();
		setTitle(title);
		setItems(items);
	}
	
	public String item(int which) {
		return items[which];
	}

	public void init() {
		dialog = new AlertDialog.Builder(context);
		dialog.setTitle("弹出窗");
	}

	public void setReturnView(View v) {
		if (v.getClass().equals(TextView.class)) {
			setNull();
			textView = ((TextView) v);
		} else if (v.getClass().equals(Button.class)) {
			setNull();
			button = ((Button) v);
		}
	}
	
	public void setClearView(View v){
		setNull();
		clearStore=((TextView)v);
	}

	public int getWhich(){
		return which;
	}
	
	public void setTitle(String title) {
		dialog.setTitle(title);
	}
	
	public void setContentView(View v){
		dialog.setView(v);
	}
	public void setEditTextDialog(){
		et=new EditText(context);
		et.setPadding(20, 20,20,20);
		et.setLines(8);
		et.setGravity(Gravity.TOP);
		dialog.setNegativeButton("取消", EditListener);
		dialog.setPositiveButton("确认", EditListener);
		setContentView(et);
	}
	public void setContentView(int ViewId){
		View v=LayoutInflater.from(context).inflate(ViewId, null);
		dialog.setView(v);
	}
	public void setItems(String[] items) {
		if (items != null) {
			this.items = items;
		}
		setView();
	}

	private void setView() {
		if (listener != null) {
			dialog.setItems(items, listener);
		}
	}

	private void setNull(){
		textView=null;
		button=null;
		clearStore=null;
	}
	
	public void show(){
		dialog.create();
		dialog.show();
	}
	public void setListener(OnClickListener listener) {
		this.listener = listener;
	}
}
